
var G_CONFIG = {
	url : {
		jumpToIndex : "shops/gtindex.html", //跳到首页
		newsDetail : "/new/d/shops/call/jh_client.php?action=NoticeDetail", //新闻动态详情
		newsList : "/new/d/shops/call/jh_client.php?action=NoticeList", //新闻动态列表
		//jumpToNewsDetail : "http://glyvc.12301.cc/new/d/shops/gnewsdetail.html", //跳转到新闻动态详情页 
		jumpToNewsDetail : (function(){
			var domain = document.domain;
			var api = "/new/d/shops/gnewsdetail_guilin.html";
			return "http://" + domain + api;
		})(),
	},
	ajaxtimeout : 8 * 1000
};


var G = {
	isEmpty : function(obj){
		for(var i in obj){
			return false;
		}
		return true;
	},
	isPositiveNum : function(count){ //判断文本框输入数值是否为正整数
		count = String(count);
		var type="^[0-9]*[1-9][0-9]*$"; 
        var re = new RegExp(type); 
        if(count.match(re) == null){ 
			return false;
        }
		return true;
	},
	_round2 : function(number,fractionDigits){  
		with(Math){  
			return round(number*pow(10,fractionDigits))/pow(10,fractionDigits);  
		}  
	},
	UA : (function(){
		
		var Sys = {};
        var ua = navigator.userAgent.toLowerCase();
        if(ua.match(/msie/)){ //window.ActiveXObject
            Sys.ie = ua.match(/msie ([\d.]+)/)[1];
		}else if (ua.match(/firefox/)){ //document.getBoxObjectFor
            Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
        }else if(ua.match(/chrome/)){//window.MessageEvent && !document.getBoxObjectFor){
            Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1];
        }else if(ua.match(/opera/)){ //window.opera
            Sys.opera = ua.match(/opera.([\d.]+)/)[1]
        }else if (window.openDatabase){
            Sys.safari = ua.match(/version\/([\d.]+)/)[1];
		}	
		return Sys;
		
	})(),
	checkLogin : function(fns){
		fns = fns || {};
		var loaded = fns.logined || function(){};
		var unload = fns.unlogin || function(){};
		var unknow = fns.unknow || function(){};
		var val = $("#iflogin").val();
		if(val == 1){
			loaded();
		}else if(val == 0){
			unload();
		}else{
			unknow();
		}
	},
	DateDiff : function(startTime, endDate){  //sDate1和sDate2是2002-12-18格式
		if(!startTime || !endDate) return false
        var aDate, oDate1, oDate2, iDays;
       /*  aDate = sDate1.split("-");
        oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);  //转换为12-18-2002格式
        aDate = sDate2.split("-");
        oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
        iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24);  //把相差的毫秒数转换为天数 */
		/* oDate1 = new Date(sDate1).getDate().toString();
		oDate2 = new Date(sDate2).getDate().toString();
		iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24); 
		return iDays;*/
		var startTime = new Date(Date.parse(startTime.replace(/-/g,"/"))).getTime();
		var endTime = new Date(Date.parse(endDate.replace(/-/g,"/"))).getTime();
		var dates = Math.abs((startTime - endTime))/(1000*60*60*24);     
		return  dates;    
        
	},
	btime : function(stratTime,endTime) {  
		var strat = stratTime.split("-");  
		var end = endTime.split("-");  
		var sdate = new Date(strat[0], strat[1], strat[2]);  
		var edate = new Date(end[0], end[1], end[2]);  
		if (sdate.getTime() > edate.getTime()) {  
			return false;  
		}  
		return true;  
	},
	ToHtmlString : function(htmlStr){
		return toTXT(htmlStr).replace(/\&lt\;br[\&ensp\;|\&emsp\;]*[\/]?\&gt\;|\r\n|\n/g, "<br/>");
		function toTXT(str) {
			var RexStr = /\<|\>|\"|\'|\&|　| /g
			str = str.replace(RexStr,
				function (MatchStr) {
					switch (MatchStr) {
						case "<":
							return "&lt;";
							break;
						case ">":
							return "&gt;";
							break;
						case "\"":
							return "&quot;";
							break;
						case "'":
							return "&#39;";
							break;
						case "&":
							return "&amp;";
							break;
						case " ":
							return "&ensp;";
							break;
						case "　":
							return "&emsp;";
							break;
						default:
							break;
					}
				}
			)
			return str;
		}
	},
	log : function(something){
		if(console && console.log){
			console.log(something)
		}
	},
	ifLogin : function(){
		if($("#iflogin").val()==0) return false;
		return true;
	}
};


//Switch全局插件
;(function(G){
	
	function Switch(container,config){
		this.config = config = $.extend({},Switch.defaults,config);
		this.container = $(container);
		this.slideUl = this.container.find(config.slideBoxCls);
		this.navContainer = this.container.find(config.navContainerCls);
		this.navs = this.navContainer.children();
		this.pannels = this.slideUl.children();
		this.pannelsize = this.pannels.length;
		this.boxWidth = this.pannels.eq(0).outerWidth(true);
		this.prevBtn = this.container.find(config.prevBtnCls);
		this.nextBtn = this.container.find(config.nextBtnCls);
		this.autoPlay = config.autoPlay;
		this.recirle = config.recirle;
		this.curIndex = this.recirle ? 1 : 0;
		this.minIndex = 0;
		this.step = config.step;
		this.maxIndex = this.pannelsize / this.step -1;
		this.timer = null;
		this.time = config.time;
		this.speed = config.speed;
		this.effect = config.effect;
		this.recirle = config.recirle;

		this.triggerType = config.triggerType;
		this.init();
	}
	Switch.prototype = {
		init : function(){
			var that = this;
			var first,last;
			if(this.recirle && this.effect == "slide"){
				first = this.pannels.first();
				last = this.pannels.last()
				this.slideUl.append(first.clone());
				this.slideUl.prepend(last.clone());
				this.pannels = this.slideUl.children();
				this.pannelsize = this.pannels.length;
				this.maxIndex = this.pannelsize / this.step -1;
				this.slideUl.css({left:-this.step})
			}
			if(this.effect == "slide"){
				var w = 0;
				this.pannels.each(function(index,li){
					w += $(this).outerWidth(true);
				})
				this.slideUl.width(w);
			}else if(this.effect == "fade"){
				this.pannels.css({"zIndex":0,"opacity":0}).removeClass("show").eq(0).css({"zIndex":0,"opacity":1}).addClass("show");
			}
			if(this.autoPlay){
				this._setInterval();
			}
			this.prevBtn.on("click",function(e){
				clearInterval(that.timer);
				that.prev();
				return false;
			})
			this.nextBtn.on("click",function(e){
				clearInterval(that.timer);
				that.next();
				return false;
			})
			this.navContainer.on(that.triggerType,"li",function(e){
				var target = $(e.currentTarget);
				that._triggerHandler(target);
				return false;
			})
		},
		_go : function(index){
			var that = this;
			var curIndex = this.curIndex;
			var slideUl = this.slideUl;
			var step = this.step;
			var boxWidth = this.boxWidth;
			var effect = this.effect;
			var recirle = this.recirle;
			var slideUl = this.slideUl;
			var pannels = this.pannels;
			var pannelsize = this.pannelsize;
			var maxIndex = this.maxIndex;
			var minIndex = this.minIndex;
			this.navs.eq(index).addClass("active").siblings().removeClass("active");
			this.config.onSwitchBefore(curIndex,index);
			if(effect == "slide"){
				slideUl.stop().animate({left:-1*step*boxWidth*index},that.speed,function(){
					that.curIndex = index;
					if(recirle && index == maxIndex && effect == "slide"){
						that.curIndex = 1;
						slideUl.css({left:-step*boxWidth})
					}else if(recirle && index == minIndex && effect == "slide"){
						that.curIndex = maxIndex-1;
						slideUl.css({left:-step*boxWidth*that.curIndex})
					}
					that.config.onSwitchAfter(that.curIndex);
				});
			}else if(effect == "fade"){
				pannels.eq(index).css("zIndex",1).siblings().css("zIndex",0);
				pannels.eq(index).animate({opacity:1},that.speed,function(){
					pannels.removeClass("show").eq(index).addClass("show")
					that.curIndex = index;
					that.config.onSwitchAfter(that.curIndex);
				})
				pannels.filter(function(){
					return $(this).hasClass("show");
				}).stop().animate({opacity:0},that.speed,function(){})
			}
			
		},
		_triggerHandler : function(target){
			var that = this;
			clearTimeout(that.timer);
			that.slideUl.stop();
			var index = that.navs.index(target);
			if(index < 0) return false;
			that.timer = setTimeout(function(){
				that._go(index);
				that.navs.eq(index).addClass("active").siblings().removeClass("active");
			},200)
		},
		_setInterval : function(){
			var that = this;
			clearTimeout(that.timer);
			this.timer = setInterval(function(){
				that.next()
			},that.time);
		},
		next : function(){
			var curIndex = this.curIndex;
			var pannelsize = this.pannelsize;
			var maxIndex = this.maxIndex;
			var recirle = this.recirle;
			var index = 0;
			if(curIndex == maxIndex){
				index = 0;
			}else{
				index = curIndex + 1;
			}
			this._go(index);
		},
		prev : function(){
			var curIndex = this.curIndex;
			var pannelsize = this.pannelsize;
			var minIndex = this.minIndex;
			var maxIndex = this.maxIndex;
			var recirle = this.recirle;
			var index = 0;
			if(curIndex == minIndex){
				index = maxIndex;
			}else{
				index = curIndex - 1;
			}
			this._go(index);
		}
	}
	Switch.defaults = {
		"time" : "4000", //间隔4s
		"speed" : "400", //速度 
		"autoPlay" : true,
		"slideBoxCls" : "",
		"navContainerCls" : "",
		"prevBtnCls" : "",
		"nextBtnCls" : "",
		"effect" : "slide",
		"step" : 1,
		"triggerType" : "mouseover", //"mouseover" || "click"
		"recirle" : false, //循环
		"onSwitchBefore" : function(){},
		"onSwitchAfter" : function(){}
	};
	
	G.Switch = Switch;
	
})(G)




//新闻公告动态
;(function(){
	var News = {};
	var container = null;
	function getDetail(id,json){
		if(!id) return false;
		var url = G_CONFIG.url.newsDetail;
		var fn = new Function();
		var loading = json.loading || fn;
		var removeLoading = json.removeLoading || fn;
		var success = json.success || fn;
		var empty = json.empty || fn;
		var timeout = json.timeout || fn;
		var ttimeout = G_CONFIG.ajaxtimeout;
		var error = json.error || fn;
		loading();
		$.ajax({
			url : url,
			type : "GET",
			dataType : "JSON",
			timeout : ttimeout,
			data : { id : id },
			success : function(res){
				removeLoading();
				if(res){
					success(res);
				}else{
					empty(res);
				}
			},
			error : function(xhr,txt){
				removeLoading();
				if(txt == "timeout"){
					timeout();
				}else{
					error();
				}
			}
		})
	}
	function ajaxList(json){
		var json = json || {};
		var url = G_CONFIG.url.newsList;
		var fn = new Function();
		var loading = json.loading || fn;
		var removeLoading = json.removeLoading || fn;
		var success = json.success || fn;
		var empty = json.empty || fn;
		var timeout = json.timeout || fn;
		var ttimeout = G_CONFIG.ajaxtimeout;
		var error = json.error || fn;
		loading();
		$.ajax({
			url : url,
			type : "GET",
			dataType : "JSON",
			timeout : ttimeout,
			success : function(res){
				removeLoading();
				if(res && res.length){
					success(res);
				}else{
					empty(res);
				}
			},
			error : function(xhr,txt){
				removeLoading();
				if(txt == "timeout"){
					timeout();
				}else{
					error();
				}
			}
		})
	}
	function getList(callback){ //初始化请求列表
		//var wrap = $("#sucOrderList");
		var listUl = container.find(".listUl");
		var loadingTxt = '<li class="loading statusTxt">加载中...</li>';
		var emptyTxt = '<li class="empty statusTxt">返回数据为空...</li>';
		var timeoutTxt = '<li class="timeout statusTxt">请求超时，请<a class="retryBtn" style="margin-left:3px;" href="#">重试</a></li>';
		var errorTxt = '<li class="error statusTxt">请求出错，请<a class="retryBtn" style="margin-left:3px;" href="#">重试</a></li>';
		ajaxList({
			loading : function(){
				listUl.html(loadingTxt);
			},
			removeLoading : function(){
				listUl.html("");
			},
			success : function(res){
				var html = buildList(res);
				listUl.html(html);
				callback && callback(res);
				setTimeout(function(){
					//var stage = $("#newsSlideStage");
					//var listUl = $("#newsSlideListUl");
					var stage = container.find(".conWrap");
					var listUl = container.find(".listUl");
					if(listUl.height()>stage.height()){
						new S($("#newsSlideStage"));
					}
				},0)
			},
			empty : function(){
				listUl.html(emptyTxt);
			},
			timeout : function(){
				listUl.html(timeoutTxt);
			},
			error : function(){ 
				listUl.html(errorTxt);
			}
		})
	}
	function buildList(res){
		var html = "";
		for(var i in res){
			var item = res[i];
			html += '<li><div class="liCon"><a class="title ggellipsis" href="'+G_CONFIG.url.jumpToNewsDetail+'?id='+item["id"]+'" class="pn">'+item["title"]+'         '+item["btime"]+'</a></div></li>';
		}
		return html;
	}
	var S = (function(){

		function newsSlider(container,config){
			var config = this.config = $.extend({},newsSlider.defaults,config);
			this.container = container;
			this.listUl = container.children();
			this.step = config.step || this.listUl.children().first().outerHeight(true);
			this.slideSpeed = config.speed;
			this.slideInterval = config.interval;
			this.dir = config.dir.toLowerCase();
			this.timer = null;
			this.init();
		}
		newsSlider.defaults = {
			dir : "v",   //方向  h-横向   v-竖向
			speed : 400, //滚动速度
			interval : 2 //滚动间隔 单位秒
		};
		newsSlider.prototype = {
			init : function(){
				var self = this;
				this.container.hover(function(){
					self.stop();
				},function(){
					self.begin();
				})
				this.timer = setTimeout(function(){
					self.anim();
				},this.slideInterval*1000);
			},
			anim : function(){
				var self = this;
				var dir = this.dir == "v" ? "top" : "left";
				var step = this.step;
				var slideInterval = this.slideInterval;
				var speed = this.slideSpeed;
				var listUl = this.listUl;
				var css = {};
				css[dir] = -1*step;
				listUl.stop().animate(css,speed,function(){
					listUl.css("top",0).children().first().appendTo(listUl);
					clearTimeout(self.timer);
					self.timer = setTimeout(function(){
						self.anim();
					},slideInterval*1000);
				})
			},
			refresh : function(){
				var self = this;
				var css = this.dir == "v" ? {top:0} : {left:0};
				this.listUl = this.container.children();
				this.listUl.css(css).stop();
				this.timer = setTimeout(function(){
					self.anim();
				},this.slideInterval*1000)
			},
			stop : function(){
				this.listUl.stop();
				clearTimeout(this.timer);
			},
			begin : function(){
				var self = this;
				this.timer = setTimeout(function(){
					self.anim();
				},1000)
			}
		}
 
		return newsSlider;

	})();
	News.getDetail = getDetail;
	News.init = function(wrap){
		if(typeof wrap == "string"){
			container = $(wrap);
		}else{
			container = $("#sucOrderList");
		}
		getList();
	}
	G.News = News;

})();






